//
//  MaterialViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-10-10.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "MaterialViewController.h"
#import <QuickLook/QuickLook.h>

@interface MaterialViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIDocumentInteractionControllerDelegate,ASIHTTPRequestDelegate>
{
    UITableView *readTable;
    ASIHTTPRequest *queryResourceinfo;

}
@property(nonatomic,assign)NSMutableArray *dirArray;
@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;
@end

@implementation MaterialViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)setupDocumentControllerWithURL:(NSURL *)url
{
    if (self.docInteractionController == nil)
    {
        self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
        self.docInteractionController.delegate = self;
    }
    else
    {
        self.docInteractionController.URL = url;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.title = @"资料库";
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 150, whiteimage.size.width, whiteimage.size.height)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    readTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 5, lineImage.size.width-6, lineImage.size.height-8) style:UITableViewStylePlain];
    readTable.dataSource = self;
    readTable.delegate = self;
    [lineView addSubview:readTable];
    [self addSearch];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.dirArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QLPreviewController *QLPV = [[QLPreviewController alloc]init];
    QLPV.delegate = self;
    QLPV.dataSource = self;
    QLPV.currentPreviewItemIndex = indexPath.row;
    [self.navigationController pushViewController:QLPV animated:YES];
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return self.dirArray.count;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    
}
#pragma mark - QLPreviewControllerDelegate

#pragma mark - UIDocumentInteractionControllerDelegate
- (NSString *)applicationDocumentsDirectory
{
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
{
    return self;
}
#pragma mark - ASIHttpDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
     if (request == queryResourceinfo){
        id json = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"query = %@",json);
    }

}
-(void)requestFailed:(ASIHTTPRequest *)request
{
    if (request == queryResourceinfo){
        id json = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"query = %@",json);
    }

}
#pragma mark - button action
- (void)sendrequest
{
    queryResourceinfo = [APhttpUtil queryResourceInfoOfPageNum:@"123" pageSize:@"456"];
    queryResourceinfo.delegate = self;
    [queryResourceinfo startAsynchronous];
}
- (void)searchInfo:(UIButton*)btn
{
    
}
#pragma mark - end
- (void)viewDidUnload
{
	readTable = nil;
	[super viewDidUnload];
}

@end
